import * as json_ from '../../util/json_';

export class Target {
    key: string;
    overridable: boolean;
    targetRoot: string;
    targetModule: string;
    targetPackage: string;
    fileName: string;
    fileType: string;
    prefix: string;
    suffix: string;
    template: string;
    enable: boolean;
    statically: boolean;
    dependencies: string[];

    constructor(key, overridable, targetRoot, targetModule, targetPackage, fileName, fileType, prefix, suffix, template, enable, statically, dependencies) {
        this.key = key;
        this.overridable = overridable;
        this.targetRoot = targetRoot;
        this.targetModule = targetModule;
        this.targetPackage = targetPackage;
        this.fileName = fileName;
        this.fileType = fileType;
        this.prefix = prefix || '';
        this.suffix = suffix || '';
        this.template = template;
        this.enable = enable;
        this.statically = statically;
        this.dependencies = dependencies;
    }

    getFileName() {
        return this.prefix + this.fileName + this.suffix + '.' + this.fileType;
    }

    static parseJson(json) {
        let parseItem = function ({key, overridable, targetRoot, targetModule, targetPackage, fileName, fileType, prefix, suffix, template, enable, statically, dependencies}) {
            return new Target(key, overridable, targetRoot, targetModule, targetPackage, fileName, fileType, prefix, suffix, template, enable, statically, dependencies);
        };
        return json_.parseJsonRender(json, function (item) {
            return parseItem(item);
        });
    }

    static initTargets(targetsConfig) {
        const targets = [];
        for (let key in targetsConfig) {
            let item = {};
            item['key'] = key;
            Object.assign(item, targetsConfig[key]);
            targets.push(item);
        }
        return Target.parseJson(targets);
    }
}